#Health of Online Communities
#
#
#Separates data into files by namespace, and then gathers statistics

import sys
import os
import collections


def main():
	global fileList #list of prelim files sorted by namespace
	global fileList2 #files sorted by namespace and year
	
	fileList = []
	fileList2 = []
	
	createFiles()
	
	for name in fileList:	
		sortByYear(name)

#separates data into initial files of like namespaces
def createFiles():
	f = open(sys.argv[1]) #opens file written on commandline
	nameList = []	
	openDict = {}
		
	for line in f:
		pieces = line.split('\t')
		namespace = pieces[2].strip() # obtains namespace
		fileName = namespace+".txt" #makes a filename according to the namespace
		
		if fileName not in openDict:
			fileVar = open("namespaces/"+fileName,"a")
			openDict[fileName] = fileVar 
			fileList.append(fileName) #appends the filelist, to be used for later
		else:
			fileVar = openDict[fileName]
		 
		fileVar.write(line) #writes the line in the corresponding namespace file
	
	for fileVar in openDict.values(): 	
		fileVar.close()	

#further separates into files of namespace and starting year
def sortByYear(fileName):
	fileVar = open("namespaces/"+fileName,"r") #opens the prelim file
	openDict = {}


	for line in fileVar:
		pieces = line.split('\t')
		name = pieces[2].strip() #namespace
		startYear = pieces[3].split('-') #obtains start year
		fileName2 = "namespaces/years/"+name+startYear[0]+".txt" #creates new file with start year of editor and namespace
		if fileName2 not in openDict:
			fileVar2 = open(fileName2,"a")
			openDict[fileName2] = fileVar2
		else:
			fileVar2 = openDict[fileName2]
		
		fileVar2.write(line) 
		
	for fileVar2 in openDict.values():
		fileVar2.close() 
	
	fileVar.close()

main()
